<template>
  <div class="fenxiao-goods">
    <fxsHeader :type="0" :Data="fxsHeaderData"></fxsHeader>
    <div class="item-card">
      <div class="item-search">
        <search-box @search="search" :placeholder="Placeholder" :showBtn="showBtn" v-model="keyWord"></search-box>
      </div>
      <form action v-on:submit.prevent>
        <div class="select-item">
          <div class="all-class" @click="clickAllClass">
            <span class="text">{{allClassVal}}</span>
            <!-- <svg-icon icon-class="icon_right" class="all-class-icon"></svg-icon> -->
            <i class="icon-right all-class-icon"></i>
          </div>
          <!-- <div class="text">价格区间：</div> -->
          <div class="price">
            <!-- <input type="text" @keyup.enter="search" v-model="startPrice" placeholder="开始价格"/> -->
            <input type="text" @confirm="search" v-model="startPrice" placeholder="开始价格"/>
          </div>
          <div class="price">
            <!-- <input type="text" @keyup.enter="search" v-model="endPrice" placeholder="结束价格"/> -->
            <input type="text" @confirm="search" v-model="endPrice" placeholder="结束价格"/>
          </div>
          <div class="van-search-btn" @click="search">
            <van-button color="#F30C23" text="搜索"></van-button>
          </div>
        </div>
      </form>
    </div>
    <div class="goods-list" v-show="list.length">
      <div class="goods-list-title" v-if="is_agent_item == 1">
        <p>下列商品是否上架：</p>
        <div class="btn" :class="[sale == 1 ? 'cur':'']" @click="selectSaleItem(1)">批量上架</div>
        <div class="btn" :class="[sale == 2 ? 'cur':'']" @click="selectSaleItem(0)">批量下架</div>
      </div>
      <sort-search :typeList="typeList" @item="selectItem"></sort-search>
      <div class="goods-list-items">
        <van-list
          v-model="loading"
          :finished="finished"
          :error.sync="error"
          :immediate-check="immediateCheck"
          error-text="请求失败，点击重新加载"
          finished-text=""
          @load="onLoad"
        >
          <div v-for="(item, index) in list" :key="index" class="list clearfix">
            <div @click="openLink(item.link_item)" class="img fl">
              <img :src="`${item.pic_url}480x480`" v-if="item.is_compress" alt/>
              <img :src="`${item.pic_url}`" v-else alt/>
              <span v-if="item.discount && item.discount != 10">{{item.discount}}折</span>
            </div>
            <div class="g-flex fl">
              <p @click="openLink(item.link_item)" class="title"><span class="fontZiy" v-if="item.is_self_support == 1">自营</span>{{item.title}}</p>
              <p class="g-price">
                <span v-html="scaleGoodsPriceFn(item.price)">
                </span>
                <del v-if="is_display_original_price == 1" class="fontGilroy">
                  <em>&yen;</em>
                  {{item.original_price}}
                </del>
              </p>
              <span class="commission" v-if="item.is_staff_commission">
                预计{{commission_name}}：
                <em>&yen; </em>
                <em class="int fontGilroy">{{item.directly_money}}</em>
              </span>
            </div>
          </div>
        </van-list>
      </div>
    </div>
    <div v-show="!list.length && finished">
      <no-data :noData="noData"></no-data>
    </div>
    <van-popup v-model="showAllClass" round position="bottom">
      <van-picker show-toolbar :columns="columns" @cancel="onCancel" @confirm="onConfirm"/>
    </van-popup>
  </div>
</template>

<script>
  // import noDataImg from '@img/user/no_distributor.png'
  import fxsHeader from '@/views/user/staffCenter/components/staffHeader/index'
  import noData from '@/components/noData/index'
  import sortSearch from '@/views/item/itemList/listWrapper/sortSearch/index'
  import searchBox from '@/views/user/components/search/index'
  import { setFenxiaoGoodsSaleItem, getStaffGoods } from '@/api/user/fxs/fxsApi'
  import { scaleGoodsPrice } from '@/utils/index.js'
  import { openPage } from '@/utils/utils'
  // import Storage from '@/utils/store.js'

  import Vue from 'vue'
  import { Popup, Picker } from 'vant'

  Vue.use(Popup).use(Picker)
  export default Vue.extend({
    data() {
      return {
        fxsHeaderData: {
          commission_name: '',
          total_commissions: '',
          total_fee: null,
          ftitle_superior_user_order: null,
          superior_user_payment: null,
          show_fxProduct: null,
          fenxiao_goods_count: '',
          ftitle_item: '',
          next_lever_agent_count: '',
          ftitle_agents: '',
          show_myUser: null,
          next_lever_user_count: '',
          ftitle_users: '',
          show_fxOrder: null,
          fenxiao_order_count: '',
          ftitle_fx_order: ''
        },
        // 排序组件控制
        typeList: [
          {
            label: '默认',
            active: false,
            type: 0,
            prop: 'default',
            sort: 'none'
          },
          { label: '价格', active: false, type: 1, prop: 'price', sort: '' },
          {
            label: '销量',
            active: false,
            type: 2,
            prop: 'default',
            sort: ''
          },
          {
            label: '人气',
            active: false,
            type: 3,
            prop: 'default',
            sort: ''
          },
          {
            label: '',
            active: false,
            type: 4,
            prop: 'default',
            sort: 'none'
          }
        ],
        // 搜索组件控制
        Placeholder: '商品搜索：请输入商品关键字',
        showBtn: true,
        // 所有分组控制
        showAllClass: false,
        group_list: [],
        columns: [],
        allClassVal: '所有分组',
        allClassId: '-1',
        // 显示批量操作
        is_agent_item: '0',
        // 列表
        sale: 0, // 批量操作按钮选中
        has_recommend: 0, // 显示推荐按钮
        fx_good_title: '',
        is_display_original_price: '0', // 显示原价
        list: [],
        // 列表vant组件
        loading: false,
        finished: false,
        error: false,
        immediateCheck: false,
        commission_name: '',
        p: 1,
        keyWord: '',
        startPrice: '',
        endPrice: '',
        search_order: '',
        noData: {
          text: '暂无员工产品',
          img: 'https://img.wifenxiao.com/h5-wfx/images/user/no_distributor.png',
          marginTop: '10em'
        }
      }
    },
    created() {
      this.init()
    },
    methods: {
      openLink(link){
        openPage(link)
      },
      scaleGoodsPriceFn(price) {
        return scaleGoodsPrice(price)
      },
      init() {
        this.$loadingWrap.show()
        setTimeout(() => {
          this.$loadingWrap.close()
        }, 50000)
        this.onLoad()
      },
      clickAllClass() {
        this.showAllClass = true
      },
      onCancel() {
        this.showAllClass = false
      },
      onConfirm(val, idx) {
        // console.log(val)
        // console.log(idx)
        if (idx > 0) {
          this.allClassId = this.group_list[idx - 1].group_id
        } else {
          this.allClassId = '-1'
        }
        this.allClassVal = val
        this.showAllClass = false
      },
      allClassList() {
        const list = ['所有分组']
        this.group_list.map(item => {
          list.push(item.title)
        })
        this.columns = list
      },
      onLoad() {
        const data = {
          p: this.p,
          search_title: this.keyWord,
          search_group_id: this.allClassId,
          search_start_price: this.startPrice,
          search_end_price: this.endPrice,
          search_order: this.search_order
        }
        getStaffGoods(data).then(res => {
          this.loading = false
          this.$loadingWrap.close()
          if (data.p == 1) {
            if (res.status == 1) {
              // 头部和导航数据
              this.fxsHeaderData.commission_name = this.commission_name =
                res.data.commission_name
              this.fxsHeaderData.total_commissions = res.data.total_commissions
              // this.fxsHeaderData.ftitle_fx_order = res.data.ftitle_fx_order
              // 员工产品
              this.fxsHeaderData.show_fxProduct = 1
              this.fxsHeaderData.fenxiao_goods_count =
                res.data._common.fenxiao_goods_count
              this.fxsHeaderData.ftitle_item = res.data.distributor.ftitle_staff_goods
              // 分销商
              this.fxsHeaderData.next_lever_agent_count =
                res.data._common.next_lever_agent_count
              this.fxsHeaderData.ftitle_agents = res.data.distributor.ftitle_agents
              // 会员
              this.fxsHeaderData.show_myUser = 1
              this.fxsHeaderData.next_lever_user_count =
                res.data._common.next_lever_user_count
              this.fxsHeaderData.ftitle_users = res.data.distributor.ftitle_staff_user
              // 订单
              this.fxsHeaderData.show_fxOrder = 1
              this.fxsHeaderData.fenxiao_order_count =
                res.data._common.fenxiao_order_count
              this.fxsHeaderData.ftitle_fx_order = res.data.distributor.ftitle_fx_order
              // 搜索部分
              this.group_list = res.data.group_list
              // 判断上架/下架
              this.is_agent_item = res.data.is_agent_item
              // 其他
              this.has_recommend = res.data.has_recommend
              this.is_display_original_price = res.data.is_display_original_price
              this.fx_good_title = res.data.fx_good_title
              // 处理其它
              this.allClassList()
              this.typeList[4].label = this.commission_name
              // 页码为1时   初始列表
              this.list = []
              // 列表
              if (res.data.item_list.list && res.data.item_list.list.length) {
                this.list.push(...res.data.item_list.list)
                this.p++
              } else {
                this.finished = true
              }
            } else {
              this.$Error(res.msg)
            }
          } else {
            // 列表
            if (res.data.item_list.list && res.data.item_list.list.length) {
              this.list.push(...res.data.item_list.list)
              this.p++
            } else {
              this.finished = true
            }
          }
        })
      },
      getInt(price) {
        if (price) {
          const idx = price.indexOf('.')
          return idx !== -1 ? price.substr(0, idx) : price
        } else {
          return '0'
        }
      },
      getDot(price) {
        if (price) {
          const idx = price.indexOf('.')
          return idx !== -1 ? price.substr(idx) : '.00'
        } else {
          return '.00'
        }
      },
      search() {
        this.p = 1
        this.finished = false
        this.error = false
        this.loading = false
        this.$loadingWrap.show()
        setTimeout(() => {
          this.$loadingWrap.close()
        }, 50000)
        this.onLoad()
      },
      // 批量上架/下架
      selectSaleItem(type) {
        const data = {
          type: type ? 'allSale' : 'allNo',
          title: this.keyWord,
          group_id: this.allClassId,
          start_price: this.startPrice,
          end_price: this.endPrice,
          order: this.search_order
        }
        setFenxiaoGoodsSaleItem(data).then(res => {
          if (res.status == 1) {
            this.sale = type ? 1 : 2
            this.list.map(item => {
              return (item.is_sale = type)
            })
            this.$Error(`操作成功！`)
          } else {
            this.$Error(`操作失败！${res.msg}`)
          }
        })
      },
      // 上架/下架
      selectOneSaleItem(type, id, index) {
        const data = {
          type: type ? 'allNo' : 'allSale',
          id
        }
        setFenxiaoGoodsSaleItem(data).then(res => {
          if (res.status == 1) {
            this.$Error(`操作成功！`)
            this.list[index].is_sale = type ? 0 : 1
          } else {
            this.$Error(`操作失败！${res.msg}`)
          }
        })
      },
      // 推荐
      selectRecommend(type, id, index) {
        const data = {
          type: type ? 'unrecommend' : 'recommend',
          id
        }
        setFenxiaoGoodsSaleItem(data).then(res => {
          if (res.status == 1) {
            this.$Error(`操作成功！`)
            this.list[index].is_recommend = type ? 0 : 1
          } else {
            this.$Error(`操作失败！${res.msg}`)
          }
        })
      },
      // 排序方式
      selectItem({ sort, prop, type }) {
        if (type == 0) {
          // 默认
          this.search_order = null
        } else if (type == 1) {
          if (sort == 'ascending') {
            // 价格升
            this.search_order = 'down'
          } else if (sort == 'descending') {
            // 价格降
            this.search_order = 'up'
          }
        } else if (type == 2) {
          if (sort == 'ascending') {
            // 销量升
            this.search_order = 'sales_volume'
          } else if (sort == 'descending') {
            // 销量降   后台暂时没有接口
            // this.search_order = 'sales_volume'
          } else {
            this.search_order = 'sales_volume'
          }
        } else if (type == 3) {
          if (sort == 'ascending') {
            // 人气升
            this.search_order = 'pv'
          } else if (sort == 'descending') {
            // 人气降 后台暂时没有接口
            // this.search_order = 'pv'
          } else {
            this.search_order = 'pv'
          }
        } else if (type == 4) {
          // 默认
          this.search_order = 'commission'
        }
        this.search()
      },
      // Jump(itemid) {
      //   const path = '/mb/item/item-detail'
      //   this.$JumpPath(this, path, { itemid })
      // }
      JumpUrl(url) {
        window.location.href = url
      }
    },
    components: {
      fxsHeader,
      noData,
      sortSearch,
      searchBox
    },
    mounted() {
      // 小程序触底加载更多
      window.addEventListener("reachbottom", () => {
        this.onLoad()
      })
    }
  })
</script>

<style lang="scss">
  @import '../../../../styles/variables';
  @import '../../../../styles/mixin';

.fenxiao-goods {
  .item-card {
    margin: 20px 0 0;
    padding: 20px 0;
    overflow: hidden;
    background-color: $default;
    .item-search{
      padding:0 20px;
    }
    .select-item {
      padding: 20px 20px 10px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      flex-wrap: nowrap;
      div {
        // flex: 1;
        &.all-class {
          width: 170px;
          height: 66px;
          // line-height: 66px;
          color: #666;
          text-align: center;
          font-size: 24px;
          border:2px solid #EDEDED;
          display: flex;
          justify-content: center;
          align-items: center;
          .text {
            display: inline-block;
            max-width: 145px;
            @include showEllipsis;
          }
          /* .svg-icon {
            width: 20px;
            margin-left:4px;
            transform: rotateZ(90deg);
          } */
          .all-class-icon{
            margin-left: 8px;
            margin-top: -4px;
            padding: 6px;
            border-color:#999;
            transform: rotate(45deg);
          }
        }
        &.text {
          width: 120px;
          font-size: 24px;
          color: #333;
          white-space: nowrap;
        }
        &.price {
          @extend .all-class;
          display: flex;
          justify-content: center;
          align-items: center;
          input {
            width: 130px;
            height: 30px;
            border: none;
            line-height: 1.4;
          }
          &:last-child {
            margin-left: 20px;
            position: relative;
            &::after {
              content: '';
              position: absolute;
              left: -17px;
              top: 50%;
              transform: translateY(-50%);
              width: 10px;
              height: 2px;
              background-color: #666;
            }
          }
        }
        &.van-search-btn{
          .van-button{
            width:124px;
            height:60px;
            font-size:28px;
          }
        }
      }
    }
  }
  .goods-list {
    margin: 20px 0;
    padding:10px 0 20px;
    background-color: $default;
    &-title {
      padding:20px 30px 0;
      display: flex;
      align-items: center;
      justify-content: flex-start;
      color: #333;
      .btn {
        height:50px;
        margin-left:20px;
        padding:0 20px;
        color:#666;
        font-size: 22px;
        border-color:#B3B3B3;
        span {
          display: flex;
          justify-content: center;
          align-items: center;
        }
        &.cur {
          border-color:#E71F19;
          color: #F21228;
        }
      }
    }
    // .sort-search{
    //   padding:10px 0;
    // }
    &-items {
      .list {
        display:flex;
        padding-left:20px;
        background: #fff;
        .img {
          padding: 20px 0;
          display: inline-block;
          width: 190px;
          height: 230px;
          overflow: hidden;
          position: relative;
          border-radius: 12px;
          img {
            display:block;
            width: 100%;
            height: 100%;
            border-radius: 12px;
          }
          span {
            display: inline-block;
            overflow: hidden;
            position: absolute;
            top: 0px;
            left: 0px;
            color: #fff;
            background: #e4393c;
            padding: 2px 6px;
          }
        }
        .g-flex {
          flex:1;
          margin-left:20px;
          padding: 20px 20px 20px 0;
          width: 500px;
          height: 230px;
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          border-bottom:2px solid #EDEDED;
          .title {
            font-size: 26px;
            color: #333;
            @include showEllipsis;
          }
        }
        .g-price {
          display: inline-block;
          margin: 0;
          margin-top: 10px;
          span {
            color: $warning;
            em {
              font-weight: bold;
            }
          }
          del {
            margin-left: 5px;
            color: #999;
            font-size: 24px;
          }
        }
        .commission {
          font-size: 24px;
          color:#999;
          em {
            color: #FF8C01;
            margin-left: -5px;
            font-size: 26px;
            &.int {
              margin-left: -5px;
            }
          }
        }
        .g-fx-btn {
          margin-top: 10px;
          &>span {
            display: inline-block;
            margin-right:16px;
            .btn{
              height:50px;
              padding:0 24px;
              font-size:24px;
              text-align:center;
              color:#F21228;
            }
          }
        }
      }
    }
  }
}
</style>
